﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Yuans.BinaryConfig
{
    public class NumberItem : ConfigItem
    {
        public long NumValue { set; get; }

        public NumberItem(string name, long number, ParamType numberType)
        {
            base.ItemName = name;
            base.ItemType = numberType;
            this.NumValue = number;
        }

        public override string GetFriendValueDescription()
        {
            string friendDesc = "";
            switch (base.ItemType)
            {
                case ConfigItem.ParamType.NUMBER_U8:
                    {
                        friendDesc = string.Format("0x{0:X},{1:D}", (byte)NumValue, (byte)NumValue);
                    }
                    break;
                case ConfigItem.ParamType.NUMBER_I8:
                    {
                        friendDesc = string.Format("0x{0:X},{1:D}", (sbyte)NumValue, (sbyte)NumValue);
                    }
                    break;
                case ConfigItem.ParamType.NUMBER_U16:
                    {
                        friendDesc = string.Format("0x{0:X},{1:D}", (UInt16)NumValue, (UInt16)NumValue);
                    }
                    break;
                case ConfigItem.ParamType.NUMBER_I16:
                    {
                        friendDesc = string.Format("0x{0:X},{1:D}", (Int16)NumValue, (Int16)NumValue);
                    }
                    break;
                case ConfigItem.ParamType.NUMBER_U32:
                    {
                        friendDesc = string.Format("0x{0:X},{1:D}", (UInt32)NumValue, (UInt32)NumValue);
                    }
                    break;
                case ConfigItem.ParamType.NUMBER_I32:
                    {
                        friendDesc = string.Format("0x{0:X},{1:D}", (Int32)NumValue, (Int32)NumValue);
                    }
                    break;
                default:
                    break;
            }
            return friendDesc;

        }
        public override byte[] GetItemValueRawBytes()
        {
            byte[] rawBytes = new byte[0];
            switch (this.ItemType)
            {
                case ConfigItem.ParamType.NUMBER_U8:
                    {
                        rawBytes = new byte[1] { (byte)this.NumValue };
                    }
                    break;
                case ConfigItem.ParamType.NUMBER_I8:
                    {
                        rawBytes = new byte[1] { (byte)((sbyte)this.NumValue) };
                    }
                    break;
                case ConfigItem.ParamType.NUMBER_U16:
                    {
                        rawBytes = BitConverter.GetBytes((UInt16)this.NumValue);
                    }
                    break;
                case ConfigItem.ParamType.NUMBER_I16:
                    {
                        rawBytes = BitConverter.GetBytes((Int16)this.NumValue);
                    }
                    break;
                case ConfigItem.ParamType.NUMBER_U32:
                    {
                        rawBytes = BitConverter.GetBytes((UInt32)this.NumValue);
                    }
                    break;
                case ConfigItem.ParamType.NUMBER_I32:
                    {
                        rawBytes = BitConverter.GetBytes((Int32)this.NumValue);
                    }
                    break;
                default:
                    break;
            }
            return rawBytes;
        }
    }
}
